package part21;

import java.util.concurrent.TimeUnit;

/**
 * @author wcj
 * @Date 2021/6/7 17:04
 * @Version 1.0
 */
public class Part217 {

    /**
     * 可以在线程启动之前设置线程为后台线程即保护线程
     * 后台线程主要是提供一些通用服务，类似于我们需求中的非功能性需求
     *
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new SimpleDaemons());
            thread.setDaemon(true);
            thread.start();
        }
        TimeUnit.MILLISECONDS.sleep(120);
    }
}

class SimpleDaemons implements Runnable {

    @Override
    public void run() {
        try {
            while (true) {
                TimeUnit.MILLISECONDS.sleep(100);
                System.out.println(Thread.currentThread() + " " + this);
            }
        } catch (InterruptedException e) {
            System.out.println("Interrupt!");
        }
    }
}
